/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stamboom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jeroen
 */
public class DatabeesKneksie {

    String dbUrl = "jdbc:mysql://localhost/stamboom";
    Connection con;
    SimpleDateFormat sdf;

    public DatabeesKneksie() {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public boolean connect() {
        try {
            con = DriverManager.getConnection(dbUrl, "root", "");
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabeesKneksie.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean disconnect() {
        try {
            con.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabeesKneksie.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public Persoon getPersoon(int id) {

        return null;
    }

    public void savePeople(ArrayList<Persoon> people) {

        try {
            PreparedStatement psDelete = con.prepareStatement("TRUNCATE TABLE PERSOON");

            psDelete.execute();

            PreparedStatement psVullenPersoon = con.prepareStatement(
                    "INSERT IGNORE INTO Persoon VALUES(?,?,?,?,?,?,?)");

            for (Persoon p : people) {
                int persnr = p.getNr();
                String achternaam = p.getAchternaam();
                String voornamen = p.getVoornamen();
                String tussenvoegsel = p.getTussenvoegsel();
                String gebdat = p.getGebdat();
                String geslacht;
                if (p.getGeslacht() == Geslacht.MAN) {
                    geslacht = "m";
                } else {
                    geslacht = "v";
                }

                psVullenPersoon.setInt(1, persnr);
                psVullenPersoon.setString(2, achternaam);
                psVullenPersoon.setString(3, voornamen);
                psVullenPersoon.setString(4, tussenvoegsel);
                psVullenPersoon.setString(5, gebdat);
                psVullenPersoon.setString(6, geslacht);
                psVullenPersoon.setString(7, null);


                psVullenPersoon.execute();
            }

        } catch (SQLException ex) {
            Logger.getLogger(DatabeesKneksie.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveGezin(ArrayList<Gezin> ag) {
        try {
            PreparedStatement psDelete = con.prepareStatement("TRUNCATE TABLE GEZIN");

            psDelete.execute();

            PreparedStatement psVullenGezin = con.prepareStatement(
                    "INSERT IGNORE INTO Gezin VALUES(?,?,?,?,?)");

            for (Gezin g : ag) {

                psVullenGezin.setInt(3, 0);
                psVullenGezin.setString(4, "");
                psVullenGezin.setString(5, "");

                psVullenGezin.setInt(1, g.getNr());
                psVullenGezin.setInt(2, g.getOuder1().getNr());

                if (g.getOuder2() != null) {
                    psVullenGezin.setInt(3, g.getOuder2().getNr());
                }
                if (g.getHuwelijk() != null) {
                    psVullenGezin.setString(4, g.getHuwelijk().toString());
                }
                if (g.getScheiding() != null) {
                    psVullenGezin.setString(5, g.getScheiding().toString());
                }
                psVullenGezin.execute();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabeesKneksie.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
